package com.we.flink.userlabelplatform.tag;

import org.apache.flink.api.common.functions.FilterFunction;
import org.apache.flink.api.common.functions.RichFlatMapFunction;
import org.apache.flink.api.common.state.ValueState;
import org.apache.flink.api.common.state.ValueStateDescriptor;
import org.apache.flink.api.java.functions.KeySelector;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.Table;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;
import org.apache.flink.util.Collector;

import com.alibaba.fastjson.JSONObject;
import com.we.flink.userlabelplatform.table.TableOrderInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class TagOrderInfo {
    public long uid;
    public int baoguo_card_order_submit_time;
    public int tiecard_order_submit_time;
    public int tiecard_order_pay_time;
    public int biguo_card_order_submit_time;
    public int jiangfei_card_first_pay_time;
    public int jiangfei_card_pay_time;
    public int jiangfei_card_order_submit_time;
    public int updated_at;

    public TagOrderInfo() {}

    public TagOrderInfo(
            long uid,
            int baoguo_card_order_submit_time,
            int tiecard_order_submit_time,
            int tiecard_order_pay_time,
            int biguo_card_order_submit_time,
            int jiangfei_card_first_pay_time,
            int jiangfei_card_pay_time,
            int jiangfei_card_order_submit_time,
            int updated_at) {
        this.uid = uid;
        this.baoguo_card_order_submit_time = baoguo_card_order_submit_time;
        this.tiecard_order_submit_time = tiecard_order_submit_time;
        this.tiecard_order_pay_time = tiecard_order_pay_time;
        this.biguo_card_order_submit_time = biguo_card_order_submit_time;
        this.jiangfei_card_first_pay_time = jiangfei_card_first_pay_time;
        this.jiangfei_card_pay_time = jiangfei_card_pay_time;
        this.jiangfei_card_order_submit_time = jiangfei_card_order_submit_time;
        this.updated_at = updated_at;
    }

    public static TagOrderInfo init(long uid) {
        return new TagOrderInfo(uid, 0, 0, 0, 0, Integer.MAX_VALUE, 0, 0, 0);
    }

    public static Logger LOG = LoggerFactory.getLogger(TagRiskTag.class);

    public static SingleOutputStreamOperator<String> getFlatMapStream(
            StreamTableEnvironment tableEnv, StreamExecutionEnvironment env) {
        Table tableRes = tableEnv.sqlQuery(TableOrderInfo.QUERYSQL);
        DataStream<Tuple2<Boolean, TableOrderInfo>> inputStream =
                tableEnv.toRetractStream(tableRes, TableOrderInfo.class);

        SingleOutputStreamOperator<String> out =
                inputStream
                        .filter(
                                new FilterFunction<Tuple2<Boolean, TableOrderInfo>>() {
                                    @Override
                                    public boolean filter(Tuple2<Boolean, TableOrderInfo> value)
                                            throws Exception {
                                        return value.f0;
                                    }
                                })
                        .uid("TagOrderInfo-filter")
                        .keyBy(
                                new KeySelector<Tuple2<Boolean, TableOrderInfo>, Long>() {
                                    @Override
                                    public Long getKey(Tuple2<Boolean, TableOrderInfo> value)
                                            throws Exception {
                                        return value.f1.getUid();
                                    }
                                })
                        .flatMap(
                                new RichFlatMapFunction<Tuple2<Boolean, TableOrderInfo>, String>() {
                                    ValueState<TagOrderInfo> tagOrderInfoVState;

                                    @Override
                                    public void open(Configuration parameters) throws Exception {
                                        tagOrderInfoVState =
                                                getRuntimeContext()
                                                        .getState(
                                                                new ValueStateDescriptor<
                                                                        TagOrderInfo>(
                                                                        "TagOrderInfoVState",
                                                                        TagOrderInfo.class));
                                    }

                                    @Override
                                    public void flatMap(
                                            Tuple2<Boolean, TableOrderInfo> value,
                                            Collector<String> out)
                                            throws Exception {
                                        long uid = value.f1.getUid();
                                        int order_type = value.f1.getOrder_type();
                                        int updated_at = value.f1.getUpdated_at();
                                        int submit_time = value.f1.getSubmit_time();
                                        int pay_time = value.f1.getPay_time();

                                        TagOrderInfo tmpState = tagOrderInfoVState.value();
                                        TagOrderInfo curState =
                                                tmpState == null
                                                        ? TagOrderInfo.init(uid)
                                                        : tmpState;
                                        JSONObject jsonObject = new JSONObject();
                                        jsonObject.put("uid", uid);

                                        switch (order_type) {
                                            case 19:
                                                if (curState.getBaoguo_card_order_submit_time()
                                                        < submit_time) {
                                                    jsonObject.put(
                                                            "baoguo_card_order_submit_time",
                                                            submit_time);
                                                    out.collect(jsonObject.toString());

                                                    curState.baoguo_card_order_submit_time =
                                                            submit_time;
                                                    tagOrderInfoVState.update(curState);
                                                }
                                                break;
                                            case 21:
                                                if (curState.getTiecard_order_pay_time() < pay_time
                                                        || curState.getTiecard_order_submit_time()
                                                                < submit_time) {

                                                    jsonObject.put(
                                                            "tiecard_order_submit_time",
                                                            submit_time);
                                                    /** pay_time 0 -> null */
                                                    jsonObject.put(
                                                            "tiecard_order_pay_time",
                                                            (pay_time == 0 ? null : pay_time));
                                                    out.collect(jsonObject.toString());

                                                    curState.tiecard_order_submit_time =
                                                            submit_time;
                                                    curState.tiecard_order_pay_time = pay_time;
                                                    tagOrderInfoVState.update(curState);
                                                }
                                                break;
                                            case 24:
                                                if (curState.getBiguo_card_order_submit_time()
                                                        < submit_time) {
                                                    jsonObject.put(
                                                            "biguo_card_order_submit_time",
                                                            submit_time);
                                                    out.collect(jsonObject.toString());

                                                    curState.biguo_card_order_submit_time =
                                                            submit_time;
                                                    tagOrderInfoVState.update(curState);
                                                }
                                                break;
                                            case 25:
                                                if (curState.getJiangfei_card_order_submit_time()
                                                                < submit_time
                                                        || curState.getJiangfei_card_pay_time()
                                                                < pay_time) {
                                                    jsonObject.put(
                                                            "jiangfei_card_order_submit_time",
                                                            submit_time);
                                                    jsonObject.put(
                                                            "jiangfei_card_pay_time",
                                                            (pay_time == 0 ? null : pay_time));
                                                    curState.jiangfei_card_order_submit_time =
                                                            submit_time;
                                                    curState.jiangfei_card_pay_time = pay_time;
                                                }

                                                if (pay_time != 0
                                                        && curState
                                                                        .getJiangfei_card_first_pay_time()
                                                                > pay_time) {
                                                    jsonObject.put(
                                                            "jiangfei_card_first_pay_time",
                                                            pay_time);
                                                    curState.jiangfei_card_first_pay_time =
                                                            pay_time;
                                                }

                                                if (jsonObject.size() > 1) {
                                                    out.collect(jsonObject.toString());
                                                    tagOrderInfoVState.update(curState);
                                                }

                                                break;
                                            default:
                                                break;
                                        }
                                    }
                                });
        return out;
    }

    public long getUid() {
        return uid;
    }

    public int getBaoguo_card_order_submit_time() {
        return baoguo_card_order_submit_time;
    }

    public int getUpdated_at() {
        return updated_at;
    }

    public int getTiecard_order_submit_time() {
        return tiecard_order_submit_time;
    }

    public int getTiecard_order_pay_time() {
        return tiecard_order_pay_time;
    }

    public int getBiguo_card_order_submit_time() {
        return biguo_card_order_submit_time;
    }

    public int getJiangfei_card_first_pay_time() {
        return jiangfei_card_first_pay_time;
    }

    public int getJiangfei_card_pay_time() {
        return jiangfei_card_pay_time;
    }

    public int getJiangfei_card_order_submit_time() {
        return jiangfei_card_order_submit_time;
    }
}
